Garbage collection is an integral part of numerous programming languages. Modern systems supporting object-oriented programming typically allocate objects from a region of memory called the heap. When an object found in the heap becomes unreachable because all live references to the object have been removed, the object can no longer be used. However, the object continues to occupy memory space in the heap.
Garbage collection is a process that reclaims this unused memory space and makes it available to accommodate other objects. Garbage collection alleviates the burden of complex memory management from programmers by autonomously reclaiming memory spaces that are no longer used. Software developers can spend less time chasing memory management errors and develop programs with increased reliability. However, garbage collection has its costs, which include impact on performance, user perceived pauses, increased complexity, etc. To distinguish the part of the program that does useful work from that which does the garbage collection, the term mutator is sometimes used. From the garbage collector's point of view, the mutator mutates the active data structure's connectivity. In many examples, the garbage collection is performed in a “stop the world” fashion where the mutator is halted during the collection cycle. This halting of the mutator causes user perceived pauses, especially in applications where real time response is expected.
One of the most efficient types of garbage collectors is a generational garbage collector. In a generational garbage collector, new objects are allocated in a “young generation” area of the heap. If an object continues to be reachable over a specified number of garbage collection cycles, the object is promoted to one or more old generation areas of the heap. A generational garbage collector performs garbage collection more frequently on the young generation area of the heap to match typical program behavior where most newly created objects are short lived thus allowing memory space to be reclaimed quickly. Long lived objects in the old generation areas tend to persist in memory and do not need garbage collection as frequently.
Some garbage collectors also relocate reachable objects to battle fragmentation. When the garbage collection cycle occurs, objects identified as reachable are moved, or “evacuated” to designated areas of the heap. During garbage collection of the young generation, the designated areas may be the old generation, a “to-space” of the young generation, or a “survivor space” in the heap. The objects evacuated into the “to-space” or the “survivor space” may then be promoted into the old generation according to the collection policy. The collection policy on the old generation varies depending on the garbage collector. Throughput collectors may simply do full collections in each cycle and reclaim the old generation in its entirety. Low-latency collectors may collect the old generation in an incremental fashion in order to minimize user perceived pauses. In this latter case, the garbage collection on the young generation may limit how short the pause times can be.